/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// @ts-nocheck
//import gpioctl from '@system.gpioctl';
//import storage from '@system.storage';
import CommunicationKit from '@system.CommunicationKit';


export default {
    data: {
        // date
        time: "",
        date: "",
        space: " ",
        week: "",
        lastDay:"",

        // Data
        todolist: [],

        // setting
        setting: "设置",

        // history&current conditions
        HistoryLogs: "历史记录",
        CurrentConditionValue: "无人",
        CurrentCondition: "当前实况",
        UnindentifiedPerson: "有陌生人靠近",
        DoorBellNormalStatus: "无人",
        VisitingBell: "有人按铃",

        // pic route
        actualPicRoute:"/common/icon_jingbao.png",
        alertPic:"/common/icon_jingbao.png",
        cautionPic:"/common/icon_mengling.png",
        recordPic:"/common/recordicon.png",
        wifiPic:"/common/wifi_signal4.png",
        statuePic:"/common/statueicon.png",

        //wifi pic arr
        wifiPicArray:[
            "/common/wifi_notbind.png",
            "/common/wifi_signal1.png",
            "/common/wifi_signal2.png",
            "/common/wifi_signal3.png",
            "/common/wifi_signal4.png",
        ],
        wifiIndex: 4,

        //ImageDate
        isShowAnimator: false,
        switch_on: false,
        wifi:0,
        title:"",
        light0_on: false,

        //Beep
        DoorBellStatus:"NO_PERSON_APPROACH",
        Beep:"OFF",

        //color
        ActualColor:"#00D400",
        ColorPressBell:"#0C5AF6",
        ColorUnindentified:"#E84026",
        ColorNormal:"#00D400",
        DateFont:"20px",
        TimeFont:"18px",
        DateColor:"#999999",
        TimeColor:"#000000",
        ColorOpacity06:.6,
        ColorOpacity10:1,

        //status
        CurrentStatus:"NO_PERSON_APPROACH",

        onInit() {
            setTimeout(()=>{
                setInterval(()=>this.DateFlush(),1000)
            },3000);

            setTimeout(()=>{
                setInterval(()=>this.GetKey(),500)
            },3000);
        },
    },

        DateFlush() {
            const weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
            const date = new Date();
            var hours = date.getHours() >= 10 ? date.getHours(): "0" + date.getHours();
            var minutes = date.getMinutes() >= 10 ? date.getMinutes() : "0" + date.getMinutes();
            var seconds = date.getSeconds() >= 10 ? date.getSeconds() : "0" + date.getSeconds();
            this.time = hours + ":" + minutes + ":" + seconds;

            this.date = (date.getFullYear() >= 10 ? date.getFullYear() : "0" + date.getFullYear()) + "年"
                        + (date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "月"
                        + (date.getDate() >= 10 ? date.getDate() : "0" + date.getDate()) + "日";
            this.week = weeks[date.getDay()];
        },

        SetKey(key1, value1) {
            CommunicationKit.set({
                key: key1,
                value: value1,
                success: function() {
                    console.log('call storage.set success.');
                },
                fail: function(data, code) {
                    console.log('call storage.set fail, code: ' + code + ', data: ' + data);
                },
            });
        },

        GetKey() {
            CommunicationKit.get({
                key: 'storage_key',
                success: (data)=> {
                    console.log('call storage.get success: ' + data);
                    let res = JSON.parse(data)
                    if (res.DoorBellStatus) {
                        this.DoorBellStatus = res.DoorBellStatus;
                        if(res.DoorBellStatus == 'CALLING_BELL') {  //有人按铃
                            if (this.CurrentStatus != 'CALLING_BELL') {
                                this.addHisLogs(this.VisitingBell);   //添加日志
                                this.ActualColor = this.ColorPressBell;  //修改当前状态的颜色
                                this.CurrentStatus = 'CALLING_BELL';   //修改当前状态
                                this.CurrentConditionValue = this.VisitingBell;  //设置当前状态值
                                this.actualPicRoute = this.cautionPic;  //修订图片路径
                                if(res.Beep) {
                                    this.Beep = res.Beep;
                                }
                                this.showImageIcon();   //显示图片
                            }
                        } else if (res.DoorBellStatus == 'UNKNOWN_PERSON_APPROACH') {  //不明人员
                            if (this.CurrentStatus != 'UNKNOWN_PERSON_APPROACH') {
                                this.addHisLogs(this.UnindentifiedPerson);
                                this.ActualColor = this.ColorUnindentified;
                                this.CurrentStatus = 'UNKNOWN_PERSON_APPROACH';
                                this.CurrentConditionValue = this.UnindentifiedPerson;
                                this.actualPicRoute = this.alertPic;
                                if(res.Beep) {
                                    this.Beep = res.Beep;
                                }
                                this.showImageIcon();
                            }
                        } else if (res.DoorBellStatus == 'NO_PERSON_APPROACH') {  //无人靠近
                            if (this.CurrentStatus != 'NO_PERSON_APPROACH') {
                                this.CurrentStatus = 'NO_PERSON_APPROACH';
                                this.ActualColor = this.ColorNormal;
                                this.CurrentConditionValue = this.DoorBellNormalStatus;
                                this.isShowAnimator = false;  // 关闭弹窗
                            }
                        }
                    }
                    if (res.WifiSatus) {  // 修改wifi状态
                        this.wifiIndex = res.WifiSatus;
                    }
                },
                fail: function(data, code) {
                    console.log('call storage.get fail, code: ' + code + ', data: ' + data);
                },
                complete: function() {
                    // console.log('call complete');
                },
            });
        },

        // Setting action
        wifiClickAction() {
            console.log('click wifi setting icon');
            this.SetKey('SET_BIND','SOFT_AP')  // 命令值（类型String）softap绑定
        },

        // Add history logs
        addHisLogs(visitType) {
            const gotDate = new Date();
            if (this.lastDay != gotDate.getDay()) { //新增加一天和第一条数据
                this.todolist.push({
                    theDate:this.date,
                    theTime:this.date,
                    thePerson:"",
                    theFont:this.DateFont,
                    theOpacity:this.ColorOpacity06,
                });
                this.lastDay = gotDate.getDay();
            }
            //在日期后面增加这一天的记录
            this.todolist.splice(1,0,{
                theDate:this.date,
                theTime:this.time,
                thePerson:visitType,
                theFont:this.TimeFont,
                theOpacity:this.ColorOpacity10,
            });
        },

        // Touch Icon to show it
        showImageIcon() {
            if(this.Beep == "ON") {
                this.isShowAnimator = true;
            } else {
                this.isShowAnimator = false;
            }
        },

        // Touch Icon to close it
        closeImageIcon() {
            this.isShowAnimator = false;
            this.SetKey('Beep','OFF');   // 命令值（类型String）OFF --关闭响铃
        },
}
